package org.dromara.im.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.im.domain.dto.PrivateMessageDTO;
import org.dromara.im.domain.vo.PrivateMessageVO;
import org.dromara.im.service.IPrivateMessageService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message/private")
@RequiredArgsConstructor
public class PrivateMessageController {

    private final IPrivateMessageService privateMessageService;

    @PostMapping("/send")
    public R<Long> sendMessage(@Valid @RequestBody PrivateMessageDTO vo) {
        return R.ok(privateMessageService.sendMessage(vo));
    }


    @DeleteMapping("/recall/{id}")
    public R<Long> recallMessage(@NotNull(message = "消息id不能为空") @PathVariable Long id) {
        privateMessageService.recallMessage(id);
        return R.ok();
    }

    @GetMapping("/pullOfflineMessage")
    public R pullOfflineMessage(@RequestParam Long minId) {
        privateMessageService.pullOfflineMessage(minId);
        return R.ok();
    }

    @PutMapping("/readed")
    public R readedMessage(@RequestParam Long friendId) {
        privateMessageService.readedMessage(friendId);
        return R.ok();
    }

    @GetMapping("/maxReadedId")
    public R<Long> getMaxReadedId(@RequestParam Long friendId){
        return R.ok(privateMessageService.getMaxReadedId(friendId));
    }

    @GetMapping("/history")
    public R<List<PrivateMessageVO>> recallMessage(@NotNull(message = "好友id不能为空") @RequestParam Long friendId,
                                                   @NotNull(message = "页码不能为空") @RequestParam Long page,
                                                   @NotNull(message = "size不能为空") @RequestParam Long size) {
        return R.ok(privateMessageService.findHistoryMessage(friendId, page, size));
    }

}

